﻿using System;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Tests.TestObjects;

namespace SharedGenomics.Workbench.Tests.Core
{
    [TestClass]
    public class CreationPresenterInitialisationStageTests
    {
        private Rhino.Mocks.MockRepository mocks;
        private IUnityContainer container;
        private PresenterInitialisationContext context;
        private CreationPresenterInitialisationStage stage;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new Rhino.Mocks.MockRepository();

            this.container = new UnityContainer();
            this.container.RegisterType<StubPresenter>();
            this.container.RegisterInstance(this.mocks.Stub<IEventBroker>());

            this.context = new PresenterInitialisationContext();
            this.context.Container = this.container;

            this.stage = new CreationPresenterInitialisationStage();
        }

        [TestMethod]
        public void ShouldCreatePresenter()
        {
            this.context.PresenterType = typeof(StubPresenter);

            this.stage.Initialise(context);

            Assert.IsNotNull(context.Current);
        }

        [TestMethod]
        public void ShouldNotCreateIfCurrentIsNotNull()
        {
            this.context.Current = this.mocks.Stub<IPresenter>();
            this.context.PresenterType = typeof(StubPresenter);

            this.stage.Initialise(context);

            Assert.IsNotInstanceOfType(context.Current, typeof(StubPresenter));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ShouldThrowIfContextNull()
        {
            this.stage.Initialise(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ShouldThrowIfPresenterTypeInvalid()
        {
            this.context.PresenterType = typeof(string);
            this.stage.Initialise(context);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ShouldThrowIfContainerNull()
        {
            this.context.Container = null;
            this.context.PresenterType = typeof(StubPresenter);

            this.stage.Initialise(context);
        }
    }
}
